package com.hyh.shiyanshiguanli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.shiyanshiguanli.Expections.FullException;
import com.hyh.shiyanshiguanli.Expections.NullException;
import com.hyh.shiyanshiguanli.constant.MessageConstant;
import com.hyh.shiyanshiguanli.controller.libInformation;
import com.hyh.shiyanshiguanli.domain.dto.LibInformationAddDTO;
import com.hyh.shiyanshiguanli.domain.dto.LibInformationUpdateDTO;
import com.hyh.shiyanshiguanli.domain.dto.PageDTO;
import com.hyh.shiyanshiguanli.domain.entity.Classify;
import com.hyh.shiyanshiguanli.domain.entity.LibInformation;
import com.hyh.shiyanshiguanli.domain.result.PageResult;
import com.hyh.shiyanshiguanli.mapper.ClassifyMapper;
import com.hyh.shiyanshiguanli.mapper.LibInformationMapper;
import com.hyh.shiyanshiguanli.service.LibInformationService;
import com.hyh.shiyanshiguanli.utils.Chinese;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"ALL"})
@Slf4j
@Service
public class LibInformationServiceImpl implements LibInformationService {

    @Autowired
    LibInformationMapper libInformationMapper;

    @Autowired
    ClassifyMapper classifyMapper;

    @Override
    public PageResult page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        Page<LibInformation> libInformations =  libInformationMapper.page(pageDTO);
        List<LibInformation> result = libInformations.getResult();
        long total = libInformations.getTotal();

        PageResult result1 = new PageResult();
        result1.setRecodes(result);
        result1.setTotal(total);
        return result1;

    }

    @Override
    public LibInformation selectById(Integer id) {
        LibInformation libInformation = libInformationMapper.selectById(id);
        return libInformation;
    }

    @Override
    public List<Classify> selectClassify() {
        List<Classify>  classifies =  classifyMapper.selectClassify();
        return classifies;
    }

    @Override
    public void updateById(LibInformationUpdateDTO libInformationUpdateDTO) {

        if(libInformationUpdateDTO.getLibClassifyId() == null || libInformationUpdateDTO.getClose() ==null || libInformationUpdateDTO.getOpen() == null ){
            throw new NullException(MessageConstant.NULL);
        }
        LibInformation libInformation = new LibInformation();
        BeanUtils.copyProperties(libInformationUpdateDTO,libInformation);
        Classify classify = classifyMapper.selectById(libInformationUpdateDTO.getLibClassifyId());
        libInformation.setLibClassify(classify.getName());
        libInformation.setLibManager(classify.getLibManager());
        libInformation.setLibManagerId(classify.getLibManagerId());
        libInformationMapper.updateById(libInformation);
    }

    @Override
    public void add(LibInformationAddDTO libInformationAddDTO) {
        if(libInformationAddDTO.getLibClassifyId() == null || libInformationAddDTO.getClose() ==null || libInformationAddDTO.getOpen() == null ){
            throw new NullException(MessageConstant.NULL);
        }
        LibInformation libInformation = new LibInformation();
        BeanUtils.copyProperties(libInformationAddDTO,libInformation);
        Classify classify = classifyMapper.selectById(libInformationAddDTO.getLibClassifyId());
        libInformation.setLibClassify(classify.getName());
        libInformation.setLibManager(classify.getLibManager());
        libInformation.setLibManagerId(classify.getLibManagerId());

        String pinyinInitial = Chinese.getPinyinInitial(libInformation.getLibClassify());
        String first = pinyinInitial.substring(0, 1).toUpperCase();

        //取出实验室的数量
        Integer number = classify.getNumber();
        //实验室类下的加一
        classifyMapper.addNumberByLibClassifyId(libInformationAddDTO.getLibClassifyId());
        log.info("number={}",number);
        //设置编号
        String serialNumber;
        if(number >= 0 && number < 10){
            serialNumber = first+"-00" + number ;
        }
        else if(number >= 10 && number < 100){
            serialNumber = first+"-0" + number ;
        }
        else if(number >= 100 && number < 1000) {
            serialNumber = first + "-" + number;
        }
            else {
                throw new FullException(MessageConstant.FULL);
        }
        libInformation.setNumber(serialNumber);

        libInformationMapper.add(libInformation);

    }

    @Override
    public int deleteById(Integer id) {
        libInformationMapper.deleteById(id);
        return MessageConstant.SUCCESS;
    }


}
